package com.dragon.mmochat.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.MmochatDailyActivity;
import com.dragon.mmochat.model.enumType.MmochatDailyActivityType;
import com.dragon.mmochat.model.enumType.MmochatGangLevelType;
import com.dragon.mmochat.model.enumType.MmochatGangSkillType;
import com.dragon.mmochat.model.gang.MmochatGang;
import com.dragon.mmochat.model.gang.MmochatGangMember;
import com.dragon.mmochat.model.gang.MmochatGangSkill;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class MmochatGangDao extends SimpleJdbcDaoSupport {
	private static final GangRowMapper gangMapper = new GangRowMapper();

	// 创建帮派
	private static final String createGangSql = "insert into gang(name, gang_members, creat_time) "
			+ "values(?, ?, ?)";

	public void createGang(String gSon_members, String name) {
		getSimpleJdbcTemplate().update(createGangSql, name, gSon_members,
				new Timestamp(System.currentTimeMillis()));
	}

	// 查找指定名字的帮派
	private static final String getGangByNameSql = "select * from gang where name = ?";

	public MmochatGang getGangByName(String name) {
		return getSimpleJdbcTemplate().queryForObject(getGangByNameSql,
				gangMapper, name);
	}

	// 查找指定id的帮派
	private static final String getGangByIdSql = "select * from gang where id = ? and delete=false";

	public MmochatGang getGangById(int id) {
		return getSimpleJdbcTemplate().queryForObject(getGangByIdSql,
				gangMapper, id);
	}

	private static final String getGangByIdIncludeDeletedSql = "select * from gang where id = ?";

	public MmochatGang getGangByIdIncludeDeleted(int id) {
		return getSimpleJdbcTemplate().queryForObject(
				getGangByIdIncludeDeletedSql, gangMapper, id);
	}

	// 修改帮派公告
	private static final String updateGangInfoSql = "update gang set info = ? where id = ?";

	public void updateGangInfo(String newInfo, int id) {
		getSimpleJdbcTemplate().update(updateGangInfoSql, newInfo, id);
	}

	// 更新帮派成员
	private static final String updateGangMemberSql = "update gang set gang_members = ? where id = ?";

	public void updateGangMember(String members, int id) {
		getSimpleJdbcTemplate().update(updateGangMemberSql, members, id);
	}

	// 删除帮派
	private static final String deleteGangByIdSql = "update gang set delete = true where id = ?";

	public void deleteGangById(int id) {
		getSimpleJdbcTemplate().update(deleteGangByIdSql, id);
	}

	// 读取所有帮派
	private static final String getAllGangSql = "select * from gang where delete=false";

	public List<MmochatGang> getAllGang() {
		return getSimpleJdbcTemplate().query(getAllGangSql, gangMapper);
	}

	// 完成帮派任务，更新帮派成员及帮派建设度
	private static final String updateGangBuildSql = "update gang set gang_members = ?, "
			+ "gang_build_value = gang_build_value + ?, gang_money = gang_money + ? where id = ?";

	public void updateAfterGangTask(String members, int addBuildValue,
			int money, int id) {
		getSimpleJdbcTemplate().update(updateGangBuildSql, members,
				addBuildValue, money, id);

	}

	// 重置帮派成员每周帮贡
	private static final String resetGangMemberWeekBuildValueSql = "update gang set gang_members = ?,"
			+ " lasttime_reset_weekbuildvalue = ? where id = ?";

	public void resetGangMemberWeekBuildValue(String members, long time, int id) {
		getSimpleJdbcTemplate().update(resetGangMemberWeekBuildValueSql,
				members, time, id);
	}

	// 研发帮派技能
	private static final String developGangSkillSql = "update gang set gang_build_value = gang_build_value - ?, "
			+ "gang_skills = ? where id = ?";

	public void developGangSkill(int buildUsed, String skills, int id) {
		getSimpleJdbcTemplate().update(developGangSkillSql, buildUsed, skills,
				id);
	}

	// 更新帮派等级
	private static final String updateGangLevelSql = "update gang set level = ? where id = ?";

	public void updateGangLevel(int level, int id) {
		getSimpleJdbcTemplate().update(updateGangLevelSql, level, id);
	}

	// 帮派大乱斗结束，批量更新帮派成员及帮派建设度，帮派资金
	private static final String batchUpdateAfterGangPkGameSql = "update gang set gang_members = ?, "
			+ "gang_build_value = ?, gang_money = ? where id = ?";

	public void batchUpdateAfterGangPkGame(List<Object[]> args) {
		getSimpleJdbcTemplate()
				.batchUpdate(batchUpdateAfterGangPkGameSql, args);
	}

	// 扣除帮派资金
	private static final String delGangMoneySql = "update gang set gang_money = gang_money - ? where id = ?";

	public void delGangMoney(int delMoney, int gangId) {
		getSimpleJdbcTemplate().update(delGangMoneySql, delMoney, gangId);
	}

	// 读取gang表总记录数
	public static final String getGangNumSql = "select nextval('gang_id_seq'::regclass)";

	public int getGangNum() {
		return getSimpleJdbcTemplate().queryForInt(getGangNumSql);
	}

	// 合服时读取所有帮派
	private static final String getAllGangOfMergeServerSql = "select * from gang_other";

	public List<MmochatGang> getAllGangOfMergeServer() {
		return getSimpleJdbcTemplate().query(getAllGangOfMergeServerSql,
				gangMapper);
	}

	// 更新合服帮派成员
	private static final String updateGangMemberOfMergeServerSql = "update gang_other"
			+ " set gang_members = ? where id = ?";

	public void updateGangMemberOfMergeServer(String members, int id) {
		getSimpleJdbcTemplate().update(updateGangMemberOfMergeServerSql,
				members, id);
	}

	// 更新每日活动
	private static final String updateGangFuBenInfoSql = "update gang set "
			+ "daily_activity = ? where id = ?";

	public void updateGangFuBenInfo(String activity, int gangId) {
		getSimpleJdbcTemplate()
				.update(updateGangFuBenInfoSql, activity, gangId);
	}

	// 更新帮派建设度和帮派资金
	private static final String updateGangBuildAndMoneySql = "update gang set "
			+ "gang_build_value = gang_build_value + ?, gang_money = gang_money + ? where id = ?";

	public void updateGangBuildAndMoney(int addBuildValue, int money, int id) {
		getSimpleJdbcTemplate().update(updateGangBuildAndMoneySql,
				addBuildValue, money, id);
	}

	public int commonUpdate(String sql) {
		return getSimpleJdbcTemplate().update(sql);
	}

	public List<MmochatGang> commonQuery(String sql) {
		return getSimpleJdbcTemplate().query(sql, gangMapper);
	}

	public MmochatGang commonQueryForObject(String sql) {
		return getSimpleJdbcTemplate().queryForObject(sql, gangMapper);
	}

	static class GangRowMapper implements ParameterizedRowMapper<MmochatGang> {
		@Override
		public MmochatGang mapRow(ResultSet rs, int rowNum) throws SQLException {
			MmochatGang gang = new MmochatGang();
			gang.setId(rs.getInt("id"));
			gang.setMapId(gang.getId() + MmochatConstant.mapId_gangBase);
			gang.setRoomA_mapId(gang.getId()
					+ MmochatConstant.mapId_gangRoomABase);
			gang.setRoomB_mapId(gang.getId()
					+ MmochatConstant.mapId_gangRoomBBase);
			gang.setName(rs.getString("name"));
			gang.setLevel(MmochatGangLevelType.get(rs.getInt("level")));
			gang.setBuildValue(rs.getInt("gang_build_value"));
			gang.setMoney(rs.getInt("gang_money"));
			gang.setInfo(rs.getString("info"));
			gang.setLastDayClearMembersWeekBuildValue(new Date(rs
					.getLong("lasttime_reset_weekbuildvalue")));
			gang.setDelete(rs.getBoolean("delete"));

			// TODO:GSON
			Gson gson = new Gson();

			String str = rs.getString("gang_members");
			if (str != null) {
				Map<Integer, MmochatGangMember> members = gson.fromJson(str,
						new TypeToken<Map<Integer, MmochatGangMember>>() {
						}.getType());
				if (members != null) {
					gang.setMembers(members);
				}
			}

			str = rs.getString("gang_skills");
			if (str != null) {
				Map<MmochatGangSkillType, MmochatGangSkill> skills = gson
						.fromJson(
								str,
								new TypeToken<Map<MmochatGangSkillType, MmochatGangSkill>>() {
								}.getType());
				if (skills != null) {
					gang.setSkills(skills);
				}
			}

			str = rs.getString("daily_activity");
			if (str != null) {
				Map<MmochatDailyActivityType, MmochatDailyActivity> daily_activity = gson
						.fromJson(
								str,
								new TypeToken<Map<MmochatDailyActivityType, MmochatDailyActivity>>() {
								}.getType());
				if (daily_activity != null) {
					gang.setDailyActivities(daily_activity);
				}
			}
			return gang;
		}

	}
}
